<?php
class database extends supplier {

    private $tab = array( 'optimize' => '数据优化', 'back' => '数据备份', 'restore' => '数据恢复', 'sql' => '运行SQL' );

    public function index( $c, $a ) {
        $type = G( 'type', 'optimize' );
        $method = "{$a}_{$type}";
        $this->$method( $c, $a, $type );
    }

    private function index_optimize( $c, $a, $type ) {
        $tables = $this->get_tables();
        include $this->display( $c );
    }

    private function index_back( $c, $a, $type ) {
        $this->index_optimize( $c, $a, $type );
    }

    private function index_restore( $c, $a, $type ) {
        $list = glob( WEBROOT . "content/backup/*.sql" );
        foreach ( $list as $k => $v ) {
            $backFiles[ $k ][ 'size' ] = filesize( $v );
            $backFiles[ $k ][ 'name' ] = basename( $v );
            $backFiles[ $k ][ 'time' ] = basename( $v, '.sql' );
        }
        include $this->display( $c );
    }

    private function index_sql( $c, $a, $type ) {
        include $this->display( $c );
    }

    # 删除备份文件
    public function del_back() {
        $file = WEBROOT . "content/backup/" . G( 'v' ) . '.sql';
        if ( !file_exists( $file ) ) {
            exit( "非法删除备份文件! {$file}" );
        }
        exit( @unlink( $file ) ? 'success' : '删除备份文件失败!' );
    }

    # 从备份文件中恢复数据
    public function restore() {
        $sqlfile = WEBROOT . "content/backup/" . G( 'v' ) . '.sql';
        if ( !file_exists( $sqlfile ) ) {
            exit( "备份文件不存在! {$sqlfile}" );
        }
        $sqldump = file_get_contents( $sqlfile );
        $sql = explode( ";\n", $sqldump );
        array_pop( $sql );
        foreach ( $sql as $v ) {
            $this->runSql( $v );
        }
        $this->Cache()->update();
        exit( "success" );
    }

    # 备份数据库
    public function back() {
        $tables = explode( ',', G( 'in_id' ) );
        $sqldump = '';

        $path = WEBROOT . "content/backup/";
        mkdirs( $path );
        $sqlfile = $path . time() . ".sql";

        foreach ( $tables as $table ) {
            $sql = $this->get_table_sql( $table );
            $sql = $sql ? "{$sql}\n\n" : '';
            $sqldump .= $sql;
        }
        $status = file_put_contents( $sqlfile, $sqldump ) ? 'success' : 'failed';
        chmod( $sqlfile, MODE );
        exit( $status );
    }

    # 优化数据库表
    public function optimize() {
        $sql = "OPTIMIZE TABLE " . G( 'in_id' );
        echo $this->runSql( $sql ) ? 'success' : 'failed';
    }

    # 更新全部缓存
    public function upcache() {
        $this->Cache()->update();
        exit( 'success' );
    }

    # 运行SQL语句
    public function run_sql() {
        $sql = G( 'sql' );
        exit( $this->runSql( $sql ) ? 'success' : 'failed' );
    }

    # 返回所有表信息
    private function get_tables() {
        return $this->findSql( "SHOW TABLE STATUS FROM " . dbname );
    }

    # 把数据库表转换成SQL语句
    private function get_table_sql( $table ) {

        $sql = "DROP TABLE IF EXISTS `{$table}`;\n";

        $create = $this->findSql( "SHOW CREATE TABLE {$table}" );
        $sql .= $create[ 0 ][ 'Create Table' ] . ";\n\n";

        $rows = $this->findSql( "select * from `{$table}`" );
        if ( !$rows ) {
            return $sql;
        }

        $key = "`" . implode( '`,`', array_keys( $rows[ 0 ] ) ) . "`";
        $key = "INSERT INTO `{$table}` ( {$key} ) VALUES \n";

        $val = '';
        foreach ( $rows as $v ) {
            $vals = array_values( $v );
            $tmp = '';
            foreach ( $vals as $t ) {
                $tmp[ ] = $this->escape( $t );
            }
            $val .= "(" . implode( ",", $tmp ) . "),\n";
        }
        $val = rtrim( rtrim( $val, "\n" ), "," );
        $sql .= "{$key}{$val};";
        return $sql;
    }
}